#include <stdio.h>
#include <time.h>
#include "base/abc/abc.h"
#include "base/main/main.h"
#include "opt/cut/cut.h"
#include "opt/cut/cutInt.h"
#include "app.h"
#include "opt.h"

extern Abc_Ntk_t *App_MainAlgorithm(Abc_Ntk_t *pNtk, int module, double bound, int errormodule);
/* extern int App_ExportNetWork(Abc_Ntk_t *pNtkNew ,Abc_Ntk_t *pNtkAig, AppManager * dMan,int IsMap,int IsShow);
void  mappingtable( Abc_Ntk_t *pNtk ) ;
int Abc_Cut(Cut_Man_t ** pCutMan, Abc_Ntk_t *pNtkAig);
extern void Abc_NtkShow( Abc_Ntk_t * pNtk, int fGateNames, int fSeq, int fUseReverse ); */
extern Abc_Ntk_t *App_Substitution(Abc_Ntk_t *pNtk, double bound, int errormodule);
extern Abc_Ntk_t *App_GetBestDely(Abc_Ntk_t *pNtk, double bound);
/*------------------------------------------------------------------------*/
/* 算法主程序                                                           */
/*------------------------------------------------------------------------*/
extern Abc_Ntk_t *App_MainAlgorithm(Abc_Ntk_t *pNtk, int module, double bound, int errormodule)
{
    abctime clk;
    Abc_Ntk_t *pNtk_app = NULL;
    if (!Abc_NtkIsStrash(pNtk))
    {
        Abc_Print(-1, "Cut computation is available only for AIGs (run \"strash\").\n");
        return NULL;
    }
    clk = Abc_Clock();
    //进行近似替换
    if (!module)
        pNtk_app = App_Substitution(pNtk, bound, errormodule);
    else
        pNtk_app = App_GetBestDely(pNtk, bound);
    //打印面积
    //Abc_Print(1, "Approximate Area:\t%d\n", circuit_area(pNtk_app, pCutMan, dMan));
    //打印延迟
    Abc_PrintTime(1, "Consume time", Abc_Clock() - clk);
    //打印并展示我们对原始电路进行替换的节点
    //    App_ExportNetWork(pNtk ,pNtk, dMan,0,0);
    //释放
    //Abc_NtkDelete( pNtk_app );
    // Cut_ManStop( pCutMan );
    
    return pNtk_app;
}
/* extern int App_ExportNetWork(Abc_Ntk_t *pNtkNew ,Abc_Ntk_t *pNtkAig, AppManager * dMan,int IsMap,int IsShow)
{
    if(IsMap != 0) {
        mappingtable(pNtkAig);
         }
    if(IsShow != 0) {
        Abc_NtkShow(pNtkNew, 0, 0, 1 );
         }
    return 0;
} */

/*------------------------------------------------------------------------*/
/* 输出原始网络与近似后网络对比                                          */
/*------------------------------------------------------------------------*/
/* void  mappingtable( Abc_Ntk_t *pNtk ) 
{
    int i = 0,Id1 = 0,Id2 = 0;
	Abc_Obj_t * pObj;
    FILE *fp;

	fp = fopen("maptable.txt","w");

    Abc_NtkForEachNode( pNtk, pObj, i ) {
		if(pObj->pTemp != 0) {
		    Id1 = Abc_ObjId(pObj);
            Id2 = Abc_ObjId(pObj->pTemp);
		    fprintf(fp,"%d %d\n",Id1,Id2);
		}
	}

    fclose(fp);
}
 */